﻿using System;
using System.Text;

namespace Kurax.BoardGames.Client.Engine.Commands {
    public abstract class CommandBase {
        private static readonly Crypto Crypto = new Crypto();

        protected static byte[] Encrypt(byte[] data) {
            if(data == null)
                throw new ArgumentNullException("data");
            return Crypto.Encrypt(data, GameContext.Current.Credentials.Key);
        }

        protected static byte[] Decrypt(byte[] data) {
            if(data == null)
                throw new ArgumentNullException("data");
            return Crypto.Decrypt(data, GameContext.Current.Credentials.Key);
        }

        protected static byte[] EncryptString(string data) {
            if(data == null)
                throw new ArgumentNullException("data");
            return Encrypt(Encoding.UTF8.GetBytes(data));
        }

        protected static string DecryptString(byte[] data) {
            if(data == null)
                throw new ArgumentNullException("data");
            var stringData = Crypto.Decrypt(data, GameContext.Current.Credentials.Key);
            return Encoding.UTF8.GetString(stringData, 0, stringData.Length);
        }

        public abstract int CommandId {
            get;
        }

        public short TransactionId {
            get;
            internal set;
        }

        public DateTime SendTime {
            get;
            internal set;
        }

        protected internal abstract byte[] GetBodyData();
        protected internal abstract void OnServerResponsed(byte[] data);
    }
}
